package com.guxingke.nioserver.handler;

import com.alibaba.fastjson.JSON;
import com.guxingke.nioserver.core.HttpRequest;
import com.guxingke.nioserver.core.HttpResponse;

import java.util.Map;

abstract class Handler {
    protected HttpRequest request;
    protected HttpResponse response;

    public void init(HttpRequest request, HttpResponse response) {
        this.request = request;
        this.response = response;
    }

    protected Object getArg(String key) {
        return request.getArg(key);
    }

    protected void setAttr(String key, Object value) {
        request.setAttr(key, value);
    }

    protected Map<String, Object> getAttrs() {
        return request.getAttrs();
    }

    protected void renderJson() {
        response.setContent(JSON.toJSONString(request.getAttrs()));
    }
}
